Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Output of following program?#include<stdio... Start Learning for Free
Output of following program?
#include<stdio.h>
void print(int n)
{
    if (n > 4000)
return;
printf("%d ", n);
print(2*n);
printf("%d ", n);
}
int main()
{
print(1000);
getchar();
return 0;
}
  • a)
    1000 2000 4000
  • b)
    1000 2000 4000 4000 2000 1000
  • c)
    1000 2000 4000 2000 1000
  • d)
    1000 2000 2000 1000
Correct answer is option 'B'. Can you explain this answer?
Most Upvoted Answer
Output of following program?#include<stdio.h>void print(int n){ ...
First time n=1000 Then 1000 is printed by first printf function then call print(2*1000) then again print 2000 by printf function then call print(2*2000) and it prints 4000 next time print(4000*2) is called. Here 8000 is greater than 4000 condition becomes true and it return at function(2*4000). Here n=4000 then 4000 will again print through second printf. Similarly print(2*2000) after that n=2000 then 2000 will print and come back at print(2*1000) here n=1000, so print 1000 through second printf. Option (B) is correct.
Free Test
Community Answer
Output of following program?#include<stdio.h>void print(int n){ ...

Explanation:

print() function:
- The print() function takes an integer n as input.
- It first checks if n is greater than 4000, if yes, it returns from the function.
- Then, it prints the value of n.
- It recursively calls the print() function with the value 2*n.
- Finally, it prints the value of n again.

Main function:
- In the main function, print(1000) is called.
- The print() function is called with n=1000.
- It prints 1000 and then calls print(2000).
- Inside the print(2000) function, it prints 2000 and then calls print(4000).
- Inside the print(4000) function, it prints 4000 and since 4000 is greater than 4000, it returns without printing.
- Now, as we unwind the recursion, it prints 4000 (the second time), then 2000, and finally 1000.

Therefore, the output of the program is: 1000 2000 4000 4000 2000 1000
Explore Courses for Computer Science Engineering (CSE) exam
Question Description
Output of following program?#include<stdio.h>void print(int n){ if (n > 4000)return;printf("%d ", n);print(2*n);printf("%d ", n);}int main(){print(1000);getchar();return 0;}a)1000 2000 4000b)1000 2000 4000 4000 2000 1000c)1000 2000 4000 2000 1000d)1000 2000 2000 1000Correct answer is option 'B'. Can you explain this answer? for Computer Science Engineering (CSE) 2025 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Output of following program?#include<stdio.h>void print(int n){ if (n > 4000)return;printf("%d ", n);print(2*n);printf("%d ", n);}int main(){print(1000);getchar();return 0;}a)1000 2000 4000b)1000 2000 4000 4000 2000 1000c)1000 2000 4000 2000 1000d)1000 2000 2000 1000Correct answer is option 'B'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Output of following program?#include<stdio.h>void print(int n){ if (n > 4000)return;printf("%d ", n);print(2*n);printf("%d ", n);}int main(){print(1000);getchar();return 0;}a)1000 2000 4000b)1000 2000 4000 4000 2000 1000c)1000 2000 4000 2000 1000d)1000 2000 2000 1000Correct answer is option 'B'. Can you explain this answer?.
Solutions for Output of following program?#include<stdio.h>void print(int n){ if (n > 4000)return;printf("%d ", n);print(2*n);printf("%d ", n);}int main(){print(1000);getchar();return 0;}a)1000 2000 4000b)1000 2000 4000 4000 2000 1000c)1000 2000 4000 2000 1000d)1000 2000 2000 1000Correct answer is option 'B'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of Output of following program?#include<stdio.h>void print(int n){ if (n > 4000)return;printf("%d ", n);print(2*n);printf("%d ", n);}int main(){print(1000);getchar();return 0;}a)1000 2000 4000b)1000 2000 4000 4000 2000 1000c)1000 2000 4000 2000 1000d)1000 2000 2000 1000Correct answer is option 'B'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Output of following program?#include<stdio.h>void print(int n){ if (n > 4000)return;printf("%d ", n);print(2*n);printf("%d ", n);}int main(){print(1000);getchar();return 0;}a)1000 2000 4000b)1000 2000 4000 4000 2000 1000c)1000 2000 4000 2000 1000d)1000 2000 2000 1000Correct answer is option 'B'. Can you explain this answer?, a detailed solution for Output of following program?#include<stdio.h>void print(int n){ if (n > 4000)return;printf("%d ", n);print(2*n);printf("%d ", n);}int main(){print(1000);getchar();return 0;}a)1000 2000 4000b)1000 2000 4000 4000 2000 1000c)1000 2000 4000 2000 1000d)1000 2000 2000 1000Correct answer is option 'B'. Can you explain this answer? has been provided alongside types of Output of following program?#include<stdio.h>void print(int n){ if (n > 4000)return;printf("%d ", n);print(2*n);printf("%d ", n);}int main(){print(1000);getchar();return 0;}a)1000 2000 4000b)1000 2000 4000 4000 2000 1000c)1000 2000 4000 2000 1000d)1000 2000 2000 1000Correct answer is option 'B'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Output of following program?#include<stdio.h>void print(int n){ if (n > 4000)return;printf("%d ", n);print(2*n);printf("%d ", n);}int main(){print(1000);getchar();return 0;}a)1000 2000 4000b)1000 2000 4000 4000 2000 1000c)1000 2000 4000 2000 1000d)1000 2000 2000 1000Correct answer is option 'B'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam
Signup to solve all Doubts
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev